﻿using System;
using System.Linq;
using Biz.EF;
using Biz.EF.Platform.Gxt;
using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    /// <summary>
    /// 主要处理离线、在线
    /// </summary>
    public class ProcessorStatus

    {
        private static ILog Log = LogManager.GetLogger(typeof(ProcessorData));
        /// <summary>
        /// 离线指令处理
        /// 注意：方法内已保存
        /// 是否不要保存以提高效率，让外部调用程序保存 
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process0000(PlatformGxtContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                if (!string.IsNullOrEmpty(mn))
                {
                    var items = pCtx.gateway_enterprise_info.Where(t => t.MN.Equals(mn)).ToList();
                    foreach (var item in items)
                    {
                        if (item.IsOnline.HasValue && item.IsOnline.Value == false) continue;
                        item.IsOnline = false;
                        item.DataTime = datatime;
                        pCtx.SaveChanges();
                    }
                }

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
        /// <summary>
        /// 心跳指令处理
        /// 在线处理，其实除掉0000的任何指令都表示在线
        /// 注意：方法内已保存
        /// 是否不要保存以提高效率，让外部调用程序保存 
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process9999(PlatformGxtContext pCtx, string content)
        {
            try
            {

                var mn = Parser.GetStringByKey("MN", content);
                var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                if (!string.IsNullOrEmpty(mn))
                {
                    var items = pCtx.gateway_enterprise_info.Where(t => t.MN.Equals(mn)).ToList();
                    foreach (var item in items)
                    {
                        if (!item.IsOnline.HasValue || !item.IsOnline.Value)
                        {
                            item.IsOnline = true;
                            item.DataTime = datatime;
                            pCtx.SaveChanges();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
        public static void Process3081(PlatformGxtContext pCtx, string content)
        {
            try
            {
                //var mn = Parser.GetStringByKey("MN", content);
                //var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                //if (!string.IsNullOrEmpty(mn))
                //{
                //    var items = pCtx.pi_total_water_port_map.Where(t => t.ZLMN.Equals(mn));
                //    foreach (var item in items)
                //    {
                //        if (item.IsOnline.HasValue && item.IsOnline.Value) continue;
                //        item.IsOnline = true;
                //        item.DataTime = datatime;
                //    }
                //}
                //pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
        public static void Process3082(PlatformGxtContext pCtx, string content)
        {
            try
            {
                //var mn = Parser.GetStringByKey("MN", content);
                //var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                //if (!string.IsNullOrEmpty(mn))
                //{
                //    var items = pCtx.pi_total_water_port_map.Where(t => t.ZLMN.Equals(mn));
                //    foreach (var item in items)
                //    {
                //        if (item.IsOnline.HasValue && item.IsOnline.Value) continue;
                //        item.IsOnline = true;
                //        item.DataTime = datatime;
                //    }
                //}
                //pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }

        }
        /// <summary>
        /// 同步到状态表（在线总量映射表）
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process3071(PlatformGxtContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                var per = Parser.GetFloatByKey("Per", content);
                var pump = Parser.GetStringByKey("Pump", content);
                var liquidLevel = Parser.GetStringByKey("LiquidLevel", content);
                //var datatime = Parser.GetDateTimeByDataTimeThenQnThenNow(content);
                if (!string.IsNullOrEmpty(mn))
                {
                    var items = pCtx.pi_total_water_port_map.Where(t => t.ZLMN.Equals(mn)).ToList();
                    foreach (var item in items)
                    {
                        item.Valve = per;
                        if (pump != null)
                            item.Pump = pump.Equals("ON") ? 100 : (pump.Equals("OFF") ? 0 : -1);
                        if (liquidLevel != null)
                            item.LiquidLevel = liquidLevel.Equals("High") ? 100 : (liquidLevel.Equals("Normal") ? 50 : 0);
                    }

                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
        /// <summary>
        /// 同步到指令下发表
        /// 好像没有
        /// </summary>
        /// <param name="pCtx"></param>
        /// <param name="content"></param>
        public static void Process3071_1(PlatformGxtContext pCtx, string content)
        {
            try
            {
                var mn = Parser.GetStringByKey("MN", content);
                if (string.IsNullOrEmpty(mn)) return;
                var qn = Parser.GetStringByKey("QN", content);
                if (string.IsNullOrEmpty(qn)) return;
                var per = Parser.GetDoubleByKey("Per", content);

                var items = pCtx.gateway_device_ctrl.Where(t => t.MN.Equals(mn)).Where(t => t.QN.Equals(qn)).ToList();
                foreach (var item in items)
                {
                    item.Param = per;
                    item.Result = "执行成功.";
                }
                pCtx.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
    }
}
